#!/usr/bin/env bash

# 激活Python 虚拟环境
source "$PYTHONVENVPATH/bin/activate"

rm -rf /etc/nginx/nginx.conf &&  cp "$PROJECTPATH"/nginx.conf /etc/nginx/nginx.conf && service nginx restart

# 重启服务
chown -R mysql:mysql /var/lib/mysql
chmod -R 777 /var/run/mysqld/ && chmod -R 777 /var/lib/mysql/
rm -rf /var/lib/redis/*
service redis-server restart
redis-cli ping

echo "[mysqld]
user=mysql
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock

[client]
port=3306" | sudo tee -a /etc/mysql/my.cnf

mysqld_safe --initialize-insecure --skip-grant-tables
rm -rf /var/lib/mysql/*
mysqld_safe --initialize-insecure --skip-grant-tables
service mysql restart


# mysql配置
export MYSQL_PASSWORD="123456"
MYSQL_USER="root"
MYSQL_PASSWORD="123456"

# 要创建的 数据库名 用户名 密码
DB_NAME="net"
DB_USER="net"
DB_PASS="net_123456"
# 登录 MySQL 并执行 SQL 语句
mysql <<EOF
CREATE DATABASE IF NOT EXISTS $DB_NAME;
CREATE USER IF NOT EXISTS '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASS';
GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost';
FLUSH PRIVILEGES;
EOF

sudo echo "$PROJECTPATH
$PROJECTPATH/sdk" >> /usr/lib/python3/dist-packages/custom.pth

export LANG=zh_CN.UTF-8 && echo $LANG

"$PYTHONVENVPATH/bin/python $PROJECTPATH/cron/init_db.py"

# 启动 celery 服务
#cd "$PROJECTPATH" && nohup celery -A tasks worker  --loglevel=info --pool=solo &
rm -rf "$PROJECTPATH"/logs && mkdir -p "$PROJECTPATH"/logs
cd "$PROJECTPATH" && nohup celery -A tasks worker --loglevel=info --pool=threads --concurrency=4 >> "$PROJECTPATH"/logs/worker.log 2>&1 &
cd "$PROJECTPATH" && nohup celery -A tasks beat --loglevel=info --schedule="$PROJECTPATH"/logs >> "$PROJECTPATH"/logs/beat.log 2>&1 &
cd "$PROJECTPATH" && nohup celery -A tasks flower >> "$PROJECTPATH"/logs/flower.log 2>&1 &
#cd "$PROJECTPATH" && celery -A tasks worker --loglevel=info --pool=threads --concurrency=4

# 启动 Flask 服务
#sudo "$PYTHONVENVPATH/bin/python" "$PROJECTPATH/main.py"
cd "$PROJECTPATH" && uwsgi --ini "$PROJECTPATH"/uwsgi.ini
